/**
 * 本地存储
 * @param {string} key 关键字
 * @param {Any} value 内容
 * @param {Boolean} remove 是否移除 默认false
 * @param {Boolean} clear 是否清除 默认false
 */
export function local({
  key = 'KEY',
  value,
  remove,
  clear,
}: {
  key?: string
  value?: any
  remove?: boolean
  clear?: boolean
}) {
  if (clear) return localStorage.clear()
  if (remove) return localStorage.removeItem(key)
  if (value) return localStorage.setItem(key, JSON.stringify(value))
  console.log(key, localStorage.getItem(key))
  return JSON.parse(localStorage.getItem(key) || '""') || null
}

/**
 * 窗口会话存储 临时保存同一窗口(或标签页)的数据
 * @param {string} key 关键字
 * @param {Any} value 内容
 * @param {Boolean} remove 是否移除 默认false
 * @param {Boolean} clear 是否清除 默认false
 */
export function session({
  key = 'KEY',
  value,
  remove,
  clear,
}: {
  key?: string
  value?: any
  remove?: boolean
  clear?: boolean
}) {
  if (clear) return sessionStorage.clear()
  if (remove) return sessionStorage.removeItem(key)
  if (value) return sessionStorage.setItem(key, JSON.stringify(value))
  return JSON.parse(sessionStorage.getItem(key) || '""') || null
}
